
var json = {};
var jsonlog = {};
var users=["All"];

//将消息显示在网页上
function setMessageInnerHTML(date) {
	json = JSON.parse(date);
	var e = new Date;
	var img = json.img;
	var time = e.getFullYear() + "-" + e.getMonth() + 1 + "-" + e.getDate() + "  " + e.getHours() + ":" + e.getMinutes() + ":" + e.getSeconds();
	
	users=users.concat(json.list);
	if (json.onstatus == 1) {
		var box = document.getElementById("message_box");
		var userList = document.getElementById("userList");

		for (var i = 0; i < json.onlineCount; i++) {
			if (!document.getElementById(json.list[i])) {
				if (username != json.list[i]) {
					if (username == json.list[i])
						img = "333.jpg";
					box.innerHTML += "<div id='" + json.list[i] + "' class='message_box'></div>";
					userList.innerHTML += "<li><label id='lab_" + json.list[i] + "' class='online'> </label> <a href='javascript:;' onclick='change(\"" + json.list[i] + "\")'> <img src='images/" + img + "'></a><a href='javascript:;' class='chat03_name' onclick='change(this.innerText)'>" + json.list[i] + "</a></li>";
				}
			} else if (document.getElementById("lab_" + json.list[i]).className == "offline") {
				document.getElementById("lab_" + json.list[i]).className = "online";
			}
		}
	} else if (json.onstatus == 0) {
		var InfoList;
		if (json.To == "All") {
			if (document.getElementById("href_name").innerText != "All") {
				add("All");
			}
			if (username == json.username)
				img = "333.jpg";
			InfoList = document.getElementById("All");
		} else {
			if (document.getElementById("href_name").innerText != json.username) {
				add(json.username);
			}
			InfoList = document.getElementById(json.username);
		}
		InfoList.innerHTML += "<div class='message clearfix'><div class='user-logo'><img src='images/" + img + "'/>" + "</div>" + "<div class='wrap-text'>" + "<h5 class='clearfix'>" + json.username + "</h5>" + "<div>" + json.message + "</div>" + "</div>" + "<div class='wrap-ri'>" + "<div clsss='clearfix'><span>" + time + "</span></div>" + "</div>" + "<div style='clear:both;'></div>";
	} else if (json.onstatus == -1) {
		document.getElementById("lab_" + json.username).className = "offline";
	}

	var box = document.getElementById("message_box");
	box.scrollTop = box.scrollHeight ;
}

//发送消息
function send() {
	var e = new Date;
	var time = e.getFullYear() + "-" + e.getMonth() + 1 + "-" + e.getDate() + "  " + e.getHours() + ":" + e.getMinutes() + ":" + e.getSeconds();
	var someone = document.getElementById("href_name").innerText;
	var InfoList = document.getElementById(someone);
	var Info = um.getContent();
	um.setContent("");
	if (someone != "All")
		InfoList.innerHTML += "<div class='message clearfix'><div class='user-logo'><img src='images/333.jpg'/>" + "</div>" + "<div class='wrap-text'>" + "<h5 class='clearfix'>" + username + "</h5>" + "<div>" + Info + "</div>" + "</div>" + "<div class='wrap-ri'>" + "<div clsss='clearfix'><span>" + time + "</span></div>" + "</div>" + "<div style='clear:both;'></div>";
	if (Info.length != 0)
		websocket.send(JSON.stringify({
			message : Info,
			To : someone
		}));
	var box = document.getElementById("message_box");
	box.scrollTop = box.scrollHeight ;
}

function change(value) {
	for (var x in jsonlog) {
		if (x == value) {
			window.clearInterval(jsonlog[x][0]);
			window.clearInterval(jsonlog[x][1]);
			delete json[x];
			document.getElementById("lab_" + value).className = "online";
			document.title = "zzhao";
		}
	}
	var box = document.getElementById("href_name");
	var someone = document.getElementById(box.innerText);
	someone.style.display = 'none';
	box.innerText = value;
	document.getElementById(value).style.display = 'block';
	var box = document.getElementById("message_box");
	box.scrollTop = box.scrollHeight ;
}

function add(lab) {
	var a = setInterval(function() {
		document.getElementById("lab_" + lab).className = "offline";
		setTimeout(function() {
			document.getElementById("lab_" + lab).className = "online";
		}, 500)
	}, 1000);
	var b = setInterval(function() {
		document.title = "zzhao -----【  有新消息  】";
		setTimeout(function() {
			document.title = "zzhao";
		}, 1000)
	}, 2000);
	jsonlog[lab] = [ a, b ];
}

function close() {
	closeWebSocket();
	window.close();
}